<?php
/*
 +----------------------------------------------------------------------
 + Title        : Validate 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2021/4/1 6:46
 + Last-time    : 2021/4/1 6:46+ chen2
 + Desc         : Validate
 +----------------------------------------------------------------------
*/


namespace app\common\attributes;

use app\common\validate\BaseValidate;
use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
class RandyValidate {
    public $validate;
    public $scene;
    public $is_clear;
    public function __construct(?string $validate = null , string $scene ='' , bool $is_clear = true  ) {
        $this->validate = $validate;
        $this->scene = $scene;
        $this->is_clear = $is_clear;
    }

    /**
     * @throws \ReflectionException
     * @throws \Exception
     */
    public function run(&$params){
        /**
         * @var BaseValidate $validate
         */
        $validate = (new \ReflectionClass($this->validate))->newInstance();
        //直接调用验证类
        $old_params = $params;
        $validate->checkScene($params,$this->scene,$this->is_clear);
        $validate_keys = array_keys($validate->getRule());
        foreach ($old_params as $key => $v){
            if( !in_array($key , $validate_keys)){
                error_abort(sprintf('接口入参存在不支持的参数 %s ',$key));
            }
        }
    }
}
